package tn.orange.canreminder;

import java.util.ArrayList;
import java.util.List;

import tn.orange.canreminder.helper.CalendrierHelper;
import tn.orange.canreminder.helper.CompetitionHelper;
import tn.orange.canreminder.model.Competition;
import tn.orange.canreminder.singleton.CompetitionSingleton;
import tn.orange.canreminder.sqlite.Equipe;
import tn.orange.canreminder.sqlite.EquipeDao;
import tn.orange.canreminder.sqlite.EquipeDaoImp;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServiceCompetition extends Service {

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {

		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		EquipeDao dao = new EquipeDaoImp(this);
		List<Equipe> equipes = dao.getAllEquipe();

		List<Competition> competitions;
		try {
			competitions = CompetitionSingleton.getInstance();
			for (Equipe equipe : equipes) {
				List<String> values = new ArrayList<String>();
				values.add(equipe.getNom());
				List<Competition> newCompetitions = CompetitionHelper
						.getCompetitionbyEquipe(values, competitions);
				int min = Integer.parseInt(equipe.getType());
				for (Competition competition : newCompetitions) {
					CalendrierHelper.InsertOrUpdateEvent(this, competition,
							min, "");
				}
			}
		} catch (Exception e) {

		}

		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();

	}
}